<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-input v-model="expName" placeholder="请输入试验名称" size="mini"
					style="width: 150px; margin-right: 10px"></el-input>
				<el-input v-model="expNo" placeholder="请输入试验编号" size="mini"
					style="width: 150px; margin-right: 10px"></el-input>
				<custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
				<custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
			</div>
		</navigation-bar>
		<table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" btnListWidth="60px"
			:pageSize="pageSize" :pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @finishRow="finishRow"
			@startRow="startRow"></table-index>

		<div v-if="dialogShow">
			<custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
				<form-index :formConfig="formConfig" @submit="submit" :type="type">
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
import { TestFinishFormConfig } from "@/assets/form/managementCenter/projectManage/testFinishForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import { AjaxCommon, getProject } from "@/libs/apiCommon"
import { PopupCommon } from "@/libs/popupCommon"
import pubsub from "pubsub-js"
import tableIndex from "./testFinishTable"
export default {
	components: {
		navigationBar,
		customButton,
		tableIndex,
		customDialog,
		formIndex,
	},
	data() {
		return {
			projectList: [],
			expName: "",
			expNo: "",
			// table 相关参数  begin
			dataSource: [],
			tableColumns: [
				{
					prop: "projectName",
					label: "项目名称",
				},
				{
					prop: "expName",
					label: "试验名称",
				},
				{
					prop: "expNo",
					label: "试验编号",
				},
				{
					prop: "createTime",
					label: "项目开始时间",
				},
				{
					prop: "expStatus",
					label: "项目状态",
					dictName: "exp_status",
				},
			],
			btnList: ["finish","start"],
			pageSize: 20,
			pageCount: 100,
			pageNo: 1,
			// table 相关参数  end

			// 弹窗是否显示
			dialogShow: false,
			// 弹窗标题 (新增修改不同)
			dialogTitle: "",

			// form的配置信息
			formConfig: TestFinishFormConfig,
			type: "add",
		}
	},
	methods: {
		getProjectList() {
			let self = this
			getProject("/ccdc/common/queryPmExpTree", "get").then((res) => {
				if (res.data.code == 200) {
					let newList = res.data.result
					console.log(newList, "list............")
					self.projectList = newList
					self.initPage()
				}
			})
		},
		submit(d) {
			let self = this
			if (d.id) {
				AjaxCommon("/ccdc/cfg/pm/pmExpUseDrug/edit", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			} else {
				delete d.id
				AjaxCommon("/ccdc/cfg/pm/pmExpUseDrug/add", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			}
		},
		dialogClose() {
			this.dialogShow = false
		},
		searchClick() {
			this.pageNo = 1
			this.initPage()
		},
		resetClick() {
			this.expNo = ""
			this.expName = ""
		},
		// 分页点击事件
		pageChange(p) {
			this.pageNo = p
			this.initPage()
		},
		startRow(d) {
			let self = this
			this.$prompt(
				"确定该试验已经完成配置，确认后该项目的配置信息将无法修改，请仔细核对后谨慎操作！如果确认，请输入：确认开始！",
				"温馨提示",
				{
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					inputPattern: /确认开始/,
					inputErrorMessage: "请输入确认开始",
				}
			)
				.then(({ value }) => {
					if (value.indexOf("确认开始") == -1) {
						PopupCommon.notify("info", "请输入确认开始")
						return
					}
					let data ={
						id:d.data.id,
						expStatus:"ing",
						optReason:value,
					}
					AjaxCommon("/ccdc/cfg/pm/pmExp/edit", "post", data)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initPage()
								pubsub.publish("projectChange", "修改了试验信息")
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				})
				.catch(() => {
					console.log("取消输入")
				})
		},
		finishRow(d) {
			let self = this
			this.$prompt(
				"确定该项目的试验已经完结，确认后该项目的数据将无法修改和删除，请仔细核对后谨慎操作！如果确认，请输入：确认完成！",
				"温馨提示",
				{
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					inputPattern: /确认完成/,
					inputErrorMessage: "请输入确认完成",
				}
			)
				.then(({ value }) => {

					if (value.indexOf("确认完成") == -1) {
						PopupCommon.notify("info", "请输入确认完成")
						return
					}
					let data ={
						id:d.data.id,
						expStatus:"ed",
						optReason:value,
					}
					AjaxCommon("/ccdc/cfg/pm/pmExp/edit", "post", data)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initPage()
								pubsub.publish("projectChange", "修改了试验信息")
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				})
				.catch(() => {
					console.log("取消输入")
				})
		},
		// 初始化事件
		initPage() {
			let self = this
			AjaxCommon("/ccdc/cfg/pm/pmExp/listByPage", "post", {
				expName: this.expName,
				expNo: this.expNo,
				pageNo: this.pageNo,
				pageSize: this.pageSize,
			}).then((res) => {
				if (res.data.code == 200) {
					let newData = res.data.result.records
					if (newData.length > 0) {
						newData.map((t) => {
							t.projectName = self.projectList.find(
								(b) => b.id == t.pmId
							).label
						})
					}
					console.log(newData, "newData.....")
					self.dataSource = newData
					self.pageCount = res.data.result.total
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
	},
	mounted() {
		this.getProjectList()
	},
}
</script>

<style></style>
